﻿using System;
using System.Net;
using Chat = System.Net;
using System.ComponentModel;

namespace ChatClientApp.Classes
{
    public class ClientClass
    {
        static Chat.Sockets.TcpClient tcpClient;
        string nick, pw;
        int serverPort;

        System.ComponentModel.BackgroundWorker bgWorker;
        ClientLogin cl;
        bool isLogedIn;
        bool finishedLogin;
        public string[] contactList;

        public delegate void SendMessageEventHandler(object sender, MessageEventArgs mEA);
        public static event SendMessageEventHandler OnSendMessageHandler;

        public bool FinishedLogin
        {
            get { return finishedLogin; }
        }

        public bool IsLogedIn
        {
            get { return isLogedIn; }
        }

        public string Nick
        {
            get { return nick; }
        }

        public ClientClass(string Nick, string Pw)
        {
            this.isLogedIn = false;
            this.nick = Nick;
            this.pw = Pw;
            this.finishedLogin = false;
        }

        public int ServerPort
        {
            get { return serverPort; }
            set { serverPort = value; }
        }

        public void Login()
        {
            bgWorker = new System.ComponentModel.BackgroundWorker();
            bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWorkLogin);
            bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWorker_RunWorkerCompletedLogin);
            if (getServerConnection())
                bgWorker.RunWorkerAsync();
        }

        public void Logout()
        {
            bgWorker = new System.ComponentModel.BackgroundWorker();
            bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWorkLogout);
            bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWorker_RunWorkerCompletedLogout);
            if (getServerConnection())
                bgWorker.RunWorkerAsync();
        }

        private bool getServerConnection()
        {
            raiseEvent("Trying to connect...");
            try
            {
                string hostname = Tools.Tools.HOSTNAME;
                IPAddress[] adresslist = Dns.GetHostAddresses(hostname);
                foreach (IPAddress ip in adresslist)
                {
                    if (ip.AddressFamily == Chat.Sockets.AddressFamily.InterNetwork)
                    {
                        tcpClient = new Chat.Sockets.TcpClient();
                        tcpClient.Connect(ip, 1500);
                        cl = new ClientLogin(nick, pw, serverPort, tcpClient);
                        return true;
                    }
                }
            }
            catch (Exception e)
            {
                raiseEvent(e.Message.ToString());
            }
            return false;
        }

        private void raiseEvent(String mess)
        {
            ClientLogin.OnSendMessageHandler += new ClientLogin.SendMessageEventHandler(ClientLogout_OnSendMessageHandler);
            this.OnSendMessage(new MessageEventArgs(mess));
            ClientLogin.OnSendMessageHandler -= new ClientLogin.SendMessageEventHandler(ClientLogout_OnSendMessageHandler);
        }

        protected void OnSendMessage(MessageEventArgs e)
        {
            if (OnSendMessageHandler != null)
                OnSendMessageHandler(this, e);
        }

        void ClientLogin_OnSendMessageHandler(object sender, MessageEventArgs e)
        {
            this.OnSendMessage(e);
        }

        void ClientLogout_OnSendMessageHandler(object sender, MessageEventArgs e)
        {
            this.OnSendMessage(e);
        }

        void bgWorker_DoWorkLogin(object sender, DoWorkEventArgs e)
        {
            ClientLogin.OnSendMessageHandler += new ClientLogin.SendMessageEventHandler(ClientLogout_OnSendMessageHandler);
            cl.Login();
            this.contactList = cl.ContactList;
            ClientLogin.OnSendMessageHandler -= new ClientLogin.SendMessageEventHandler(ClientLogout_OnSendMessageHandler);
            bgWorker_RunWorkerCompletedLogin(sender, new RunWorkerCompletedEventArgs("FINISHED",null,false));
        }

        void bgWorker_DoWorkLogout(object sender, DoWorkEventArgs e)
        {
            ClientLogin.OnSendMessageHandler += new ClientLogin.SendMessageEventHandler(ClientLogout_OnSendMessageHandler);
            cl.Logout();
            ClientLogin.OnSendMessageHandler -= new ClientLogin.SendMessageEventHandler(ClientLogout_OnSendMessageHandler);
            bgWorker_RunWorkerCompletedLogout(sender, new RunWorkerCompletedEventArgs("FINISHED", null, false));
        }

        void bgWorker_RunWorkerCompletedLogin(object sender, RunWorkerCompletedEventArgs e)
        {
            isLogedIn = cl.IsLogedIn;
            finishedLogin = cl.FinishedLogin;
            cl.Dispose();
        }

        void bgWorker_RunWorkerCompletedLogout(object sender, RunWorkerCompletedEventArgs e)
        {
            isLogedIn = cl.IsLogedIn;
            finishedLogin = cl.FinishedLogin;
            cl.Dispose();
        }
    }
}
